package com.init.system.controller;

import com.init.system.common.result.BaseResponse;
import com.init.system.common.result.Result;
import com.init.system.common.result.ResultUtils;
import com.init.system.model.dto.FileInfo;
import com.init.system.service.OssService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件控制层
 */
@Tag(name = "07.文件接口")
@RestController
@RequestMapping("/api/v1/files")
@RequiredArgsConstructor
public class FileController {

    private final OssService ossService;

    @PostMapping
    @Operation(summary = "文件上传")
    public BaseResponse<FileInfo> uploadFile(
            @Parameter(description = "表单文件对象") @RequestParam(value = "file") MultipartFile[] file
    ) {
        FileInfo fileInfo = ossService.uploadFile(file[0]);
        return ResultUtils.success(fileInfo);
    }
    @DeleteMapping
    @Operation(summary = "文件删除")
    @SneakyThrows
    public BaseResponse<?> deleteFile(
            @Parameter(description = "文件路径") @RequestParam String filePath
    ) {
        boolean result = ossService.deleteFile(filePath);
        Result<Object> judge = Result.judge(result);
        return ResultUtils.success(judge);
    }
}
